// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    ctor(){
        this.slideMinY = -182;
        this.slideMaxW = 324;
        this.slideMinW = 114;  
        // px / s
        this.moveSpeed = -182
        this.FPS = 60;
        this.maxLen = 7;

        this.move = false;
    },
    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:
    onCollisionEnter: function (other, self) {
        if(self.node.group == 'outside'){
            this.move = false;
            this.node.emit('outsideCollide', {node: this.node, tag: other.tag})
        }
    },
    onLoad () {
        this.setStyle();
        if(this.node.group == 'outside'){
            this.node.width = 50;
            cc.tween(this.node).to(1, {
                width: 324
            }).call(()=>{
                console.log('开始移动')
                this.startMove()
            }).start()
        }else{
            this.node.width = (this.node.posIndex/this.maxLen)*(this.slideMaxW-this.slideMinW) + this.slideMinW;
        }
        this.node.on('move', ()=>{
            //debugger;
            this.node.group = 'outside';
            this.move = true;
        }, this)
    },
    startMove(){
        this.move = true;
    },
    setStyle(){
        let color = this.node.style.color;
        this.node.color = color;
        this.node.children.forEach(item=>{
            item.color = color;
        })
    },
    start () {

    },

    update (dt) {
        this.FPS = 1/dt;
        if(this.move){
            this.node.y+=this.moveSpeed/this.FPS;
            this.node.width = (1-this.node.y/this.slideMinY)*(this.slideMaxW-this.slideMinW) + this.slideMinW
        }
    },
});
